#include "stdafx.h"
#include "EnemySpawner.h"
#include "EntityFactory.h"
#include "EntityManager.h"
#include "Enemy.h"

EnemySpawner::EnemySpawner(const EEntityType& entity_type, EntityFactory* entity_factory, EntityManager* entity_manager) 
	: Entity(entity_type), m_entity_factory(entity_factory), m_entity_manager(entity_manager)
{
}

EnemySpawner::~EnemySpawner(void)
{
}

void EnemySpawner::Init(SceneObject* scene_object, Grid* grid)
{
	m_scene_object = scene_object;
	m_grid = grid;
	m_spawn_time = 2.0f;
	m_min_spawn_time = 1.0f;
	m_max_spawn_time = 2.0f;
}

void EnemySpawner::Update(float deltatime)
{
	if (m_spawn_time <= 0.0f)
	{
		Spawn();
		SetSpawnTime();
	}
	else
		m_spawn_time -= deltatime;

	m_scene_object->m_transform = DirectX::XMMatrixTranslation(m_position.x, m_position.y, m_position.z);
	m_scene_object->m_bounding_sphere.m_position = Vec3(m_position.x, m_position.y, m_position.z);
}

void EnemySpawner::SetSpawnIntervall(float min, float max)
{
	m_min_spawn_time = min;
	m_max_spawn_time = max;
}

void EnemySpawner::AddWaypoint(Vector3 waypoint)
{
	m_waypoint.AddWaypoint(waypoint);
}

void EnemySpawner::Spawn()
{
	Entity* enemy = m_entity_factory->Create(Enemy_Type, m_grid);
	m_entity_manager->AddEntity(enemy);
	enemy->SetPosition(Vector3(m_position.x, m_position.y + 1.3f, m_position.z));

	if (enemy->GetEntityType() == Enemy_Type)
	{
		static_cast<Enemy*>(enemy)->AddWaypoint(m_waypoint);
	}
	enemy->Update(0.0f);
}

void EnemySpawner::SetSpawnTime()
{
	m_spawn_time = Random::Random(m_min_spawn_time, m_max_spawn_time);
}